from django.core.exceptions import ObjectDoesNotExist
from django.utils.feedgenerator import Atom1Feed
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.contrib.syndication.feeds import Feed
from dolist.apps.tip.models import *

current_site = Site.objects.get_current().name

class LatestTipsFeed(Feed):
    """
    Feed of the most recently published Snippets.
    
    """
    feed_type = Atom1Feed
##    title_template = 'feeds/title.html'
##    description_template = 'feeds/description.html'
    item_copyright = 'Freely redistributable'
    title = "%s: Latest tips" % current_site
    link = "/mytips/"
    description = "tips"
    author = "not set"
    def __init__(self, slug, feed_url,user):
        self.user=user
        Feed.__init__(self,slug,feed_url)
        
    def items(self):
        return Tip.objects.filter(author=self.user)[:15]
    
    def item_author_name(self, item):
        return item.author.username
    
    def item_link(self, item):
        return item.get_absolute_url()
    
    def item_pubdate(self, item):
        return item.pub_date


class TipsByAuthorFeed(Feed):
    """
    Feed of the most recent Snippets by a given author.
    
    """
    feed_type = Atom1Feed
##    title_template = 'cab/feeds/title.html'
##    description_template = 'cab/feeds/description.html'
    item_copyright = 'Freely redistributable'
    
    def __init__(self, slug, feed_url,user):
        self.user=user
        Feed.__init__(self,slug,feed_url)

    def author_name(self, obj):
        return self.user.username
    
    def items(self, obj):
        return Tip.objects.get_by_author(self.user.username)[:15]
    
    def link(self, obj):
        return "/users/%s/" % self.user.username

    def title(self, obj):
        return "%s: Latest snippets posted by %s" % (current_site, self.user.username)
    
    def item_author_name(self, item):
        return item.author.username
    
    def item_link(self, item):
        return item.get_absolute_url()
    
    def item_pubdate(self, item):
        return item.pub_date




class TipsByTagFeed(Feed):
    """
    Feed of the most recent Snippets with a given tag.
    
    """
    feed_type = Atom1Feed
##    title_template = 'feeds/title.html'
##    description_template = 'feeds/description.html'
    item_copyright = 'Freely redistributable'
    
    def __init__(self, slug, feed_url,user):
        self.user=user
        Feed.__init__(self,slug,feed_url)

    def get_object(self, bits):
        if len(bits) != 1:
            raise ObjectDoesNotExist
        return Tag.objects.get(slug__exact=bits[0])
    
    def items(self, obj):
        return Tip.objects.filter(author=self.user,tags__slug__exact=obj.slug)[:15]
    
    def link(self, obj):
        return obj.get_absolute_url()
    
    def title(self, obj):
        return "%s: Latest tips tagged with '%s'" % (current_site, obj.name)
    
    def item_author_name(self, item):
        return item.author.username
    
    def item_link(self, item):
        return item.get_absolute_url()
    
    def item_pubdate(self, item):
        return item.pub_date
